//
//  BaseViewController.m
//  Hive
//
//  Created by 惠俊贝 on 15/9/1.
//  Copyright (c) 2015年 ZM. All rights reserved.
//

#import "BaseViewController.h"
#import <MBProgressHUD.h>

@interface BaseViewController (){
    MBProgressHUD *hud;
}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = YES;
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [self setupCustomNavgationBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - press event
- (void)backButtonPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)menuButtonPress:(id)sender {
    
}
- (void)alarmButtonPress:(id)sender {
    
}


- (void)refreshButtonPress:(id)sender {
    
}

- (void)carInfoButtonPress:(id)sender {
    
}
#pragma mark - private method
- (void)setupCustomNavgationBar {
    //添加背景图
    CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, 44+STATUS_BAR_HEIGHT);
    customNavBar= [[UIView alloc]initWithFrame:frame];
    
    UIView *colorView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44+STATUS_BAR_HEIGHT)];
    colorView.backgroundColor=kThemeOrangeColor;
    [customNavBar addSubview:colorView];
    
    
    //添加标题
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH -200)/2, 11+STATUS_BAR_HEIGHT, 200, 24)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font=[UIFont fontWithName:@"MicrosoftYaHei" size:20.0];
    titleLabel.text = self.title;
    [titleLabel setShadowColor:[UIColor darkGrayColor]];
    //        [label setShadowOffset:CGSizeMake(0, -0.5)];
    [customNavBar addSubview:titleLabel];
    
    //添加返回按钮
    backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0+STATUS_BAR_HEIGHT, 44, 44)];
    [backButton setImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal];
    //    [backButton setImage:[UIImage imageNamed:@"backButtonHighlighted.png"] forState:UIControlStateHighlighted];
    //    UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [backButton addTarget:self action:@selector(backButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    backButton.hidden = YES;
    [customNavBar addSubview:backButton];
    
    //添加菜单按钮
    menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [menuButton setFrame:CGRectMake(0, 0+STATUS_BAR_HEIGHT, 44, 44)];
    [menuButton setImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
//    [backButton setImage:[UIImage imageNamed:@"backButtonHighlighted.png"] forState:UIControlStateHighlighted];
    //    UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [menuButton addTarget:self action:@selector(menuButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    menuButton.hidden = YES;
    [customNavBar addSubview:menuButton];
    
    
    alarmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [alarmButton setFrame:CGRectMake(SCREEN_WIDTH-50-44-44, 0+STATUS_BAR_HEIGHT, 44, 44)];
    [alarmButton setImage:[UIImage imageNamed:@"map_alert"] forState:UIControlStateNormal];
//    [menuButton setImage:[UIImage imageNamed:@"learnMenuHighlight.png"] forState:UIControlStateHighlighted];
    //    UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [alarmButton addTarget:self action:@selector(alarmButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    alarmButton.hidden = YES;
    [customNavBar addSubview:alarmButton];
    
    //添加info按钮
    refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [refreshButton setFrame:CGRectMake(SCREEN_WIDTH-50-44, 0+STATUS_BAR_HEIGHT, 44, 44)];
    [refreshButton setImage:[UIImage imageNamed:@"refresh"] forState:UIControlStateNormal];
    //    [infoButton setImage:[UIImage imageNamed:@"learnMenuHighlight.png"] forState:UIControlStateHighlighted];
    [refreshButton addTarget:self action:@selector(refreshButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    refreshButton.hidden = YES;
    [customNavBar addSubview:refreshButton];
    
    //添加skip按钮
    carInfoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [carInfoButton setFrame:CGRectMake(SCREEN_WIDTH-50, 0+STATUS_BAR_HEIGHT, 44, 44)];
//    [skipButton setTitle:@"跳过" forState:UIControlStateNormal];
    [carInfoButton setImage:[UIImage imageNamed:@"hp_carlist"] forState:UIControlStateNormal];
    //    [skipButton setImage:[UIImage imageNamed:@"learnMenuHighlight.png"] forState:UIControlStateHighlighted];
    [carInfoButton addTarget:self action:@selector(carInfoButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    carInfoButton.hidden = YES;
    [customNavBar addSubview:carInfoButton];
    
    
    [self.view addSubview:customNavBar];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

@implementation BaseViewController (HUB)
- (void)showHUBMessage:(NSString *)string {
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = string;
    [self performSelector:@selector(timeout) withObject:nil afterDelay:30];
}
- (void)hideHUB {
    if (hud) {
        [hud hide:YES];
        hud = nil;
    }
}
- (void)timeout {
    if (hud&&(!hud.hidden)) {
        [hud setMode:MBProgressHUDModeText];
        [hud setLabelText:@"请求超时"];
        [self performSelector:@selector(hideHUB) withObject:nil afterDelay:2.0];
    }
}

@end
